﻿using Playnite.SDK.Models;
using Playnite.SDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImportExporter.ImportAttributes
{
    public static class ImportCompletionStatuses
    {
        public static void DoImportCompletionStatuses(IPlayniteAPI PlayniteApi, Game game, List<CompletionStatus> importedCompletionStatusList)
        {
            List<CompletionStatus> existedCompletionStatusList = PlayniteApi.Database.CompletionStatuses.ToList();

            // 在imported中根据Id找Name（这个是一定能找到的）
            List<CompletionStatus> spcificationExistsCompletionStatusList = importedCompletionStatusList.Where<CompletionStatus>(item =>
            {
                return game.CompletionStatusId == item.Id;
            }).ToList();
            // 在existed中根据Name找Id（这个不一定能找到，imported来的可能是新的）
            game.CompletionStatusId = Guid.Empty;
            foreach (CompletionStatus completionStatus in spcificationExistsCompletionStatusList)
            {
                List<CompletionStatus> foundList = existedCompletionStatusList.FindAll(p => p.Name.Trim().Equals(completionStatus.Name.Trim(), StringComparison.OrdinalIgnoreCase)).ToList();
                if (foundList.Count > 0)
                {
                    game.CompletionStatusId = foundList[0].Id;
                }
                else
                {
                    PlayniteApi.Database.CompletionStatuses.Add(completionStatus);
                    game.CompletionStatusId = completionStatus.Id;
                }
            }
        }
    }
}
